<template>
    <div class="container">
        <div class="top">
            <div>{{ title }}</div>
        </div>
        <div class="bottom">
            <el-row>
                <el-col :span="2">
                    <span class="title">分类</span>
                </el-col>
                <el-col :span="22">
                    <span v-for="(item,index) in Object.keys(categories)"
                        :class="item == category ? 'cate-sel' : 'cate'" :key="index" @click="switchCategory(item)">{{
                        item }}</span>
                </el-col>
            </el-row>
            <el-row style="height: 15px">
                <el-col :span="24">
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="2">
                    <span class="title">子分类</span>
                </el-col>
                <el-col :span="22">
                    <span v-for="(item,index) in categories[category]"
                        :class="item == subCategory ? 'cate-sel' : 'cate'" :key="index"
                        @click="switchSubCategory(item)">{{ item }}</span>
                </el-col>
            </el-row>
            <el-row style="height: 45px">
                <el-col :span="24">
                </el-col>
            </el-row>
        </div>
        <div class="title">
            {{ subCategory }}
        </div>
    </div>
</template>

<script>
let _this = null
export default {
    name: 'Category',
    props: {
        title: {
            type: String,
            default: '标题'
        },
        categories: {
            type: Object,
            default: () => {
                return {}
            }
        }
    },
    mounted() {
        _this = this
        let cate = Object.keys(this.$props.categories)[0]
        this.switchCategory(cate)
    },
    data() {
        return {
            category: '',
            subCategory: ''
        }
    },
    methods: {
        switchCategory: (e) => {
            _this.category = e
            _this.switchSubCategory(_this.$props.categories[e][0])
        },
        switchSubCategory: (e) => {
            _this.subCategory = e
            _this.$emit('changeCategory', e)
        }
    }
}
</script>

<style lang="scss" scoped>
.container {
    width: 968px;
    margin: auto;

    .top {
        font-size: 24px;
        font-weight: 400;
        padding: 40px 0 32px 0;
    }

    .bottom {
        .cate {
            opacity: 0.9;
            font-weight: 400;
            font-size: 14px;
            padding-right: 27px;
            padding-bottom: 12px;
            cursor: pointer;
        }

        .cate-sel {
            color: #007dff;
            opacity: 0.9;
            font-weight: 400;
            font-size: 14px;
            padding-right: 27px;
            padding-bottom: 12px;
            cursor: pointer;
        }

        .title {
            opacity: 0.9;
            font-weight: 400;
            font-size: 14px;
        }
    }

    .title {
        height: 28px;
        font-weight: 400;
        font-size: 20px;
    }
}
</style>